#include "statistics.h"
#include "ui_statistics.h"
#include "wode.h"
#include "taskpage.h"
#include "tomatoclock.h"
#include "mainwindow.h"
#include "achieve.h"
#include "databasemanager.h"
#include "arcwidget.h"
#include <QDate>
#include <QLabel>


Statistics::Statistics(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Statistics)
{
    ui->setupUi(this);
    Achieve g_achieve;
    g_achieve.displayfruit(ui->label_3);
    display_today_history();

    //四个菜单按钮
    ui->start->setStyleSheet(
        "QPushButton {"
        "background-color: rgba(255, 255, 255, 128);"
        "color: #91ada2;"
        "}"
        );
    ui->wode->setStyleSheet(
        "QPushButton {"
        "background-color: rgba(255, 255, 255, 128);"
        "color: #91ada2;"
        "}"
        );
    ui->task->setStyleSheet(
        "QPushButton {"
        "background-color: rgba(255, 255, 255, 128);"
        "color: #91ada2;"
        "}"
        );
    ui->pushButton_4->setStyleSheet(
        "QPushButton {"
        "background-color: rgba(255, 255, 255, 128);"
        "color: #91ada2;"
        "}"
        );

    ui->pushButton->setStyleSheet(
        "QPushButton {"
        " background-color: transparent;"
        "color: white;"
        "border: 2px solid #91ada2;"
        "}"
        );

    //@用户名
    ui->label_3->setStyleSheet(
        "QLabel {"
        " background-color: transparent;"
        "color: #cbd47f;"
        "}"
        );
    this->close();
}

Statistics::~Statistics()
{
    delete ui;
}

void Statistics::display_today_history()
{
    QDate today = QDate::currentDate();
    QString dateString = today.toString("yyyy-MM-dd");
    int UID=DatabaseManager::getUID();

    QSqlQuery query(DatabaseManager::getDatabase());

    query.prepare("select * from history WHERE date =:date AND user =:uid;");
    query.bindValue(":date", dateString);
    query.bindValue(":uid", UID);

    type_name_Data.clear();
    duration_Data.clear();
    bool hasdata=false;

    if (!query.exec()) {
        qDebug() << "Query failed:" << query.lastError().text();
    }

    while (query.next()) {
        int id = query.value("id").toInt();
        QString task_name = query.value("task_name").toString();
        QString task_type = query.value("task_type").toString();
        int duration = query.value("duration").toInt();
        QString date = query.value("date").toString();
        qDebug() << "ID:" << id << "Task Name:" << task_name << "Task Type:" << task_type << "Duration:" << duration << "Date:" << date;

        type_name_Data.append(qMakePair(id, task_name));
        duration_Data.append(qMakePair(id, duration));
        hasdata=true;
    }
    if(!hasdata)
    {
        QLabel *emptyLabel = new QLabel(ui->frame);
        QPixmap pixmap(":/images/imgs/no_data.png");
        pixmap = pixmap.scaledToWidth(300, Qt::SmoothTransformation);
        emptyLabel->setPixmap(pixmap);
        emptyLabel->setAlignment(Qt::AlignCenter);
        emptyLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
        QVBoxLayout *layout = new QVBoxLayout(ui->frame);
        layout->addWidget(emptyLabel);
        ui->frame->setLayout(layout);
    }
    else
    {
        ArcWidget *arcWidget = new ArcWidget();
        arcWidget->resize(500,450);

        for (const auto &pair : type_name_Data) {
            qDebug() << "Task ID:" << pair.first << ", Task Name:" << pair.second;
            arcWidget->type_name_Map.insert(pair.first, pair.second);
        }
        for (const auto &pair : duration_Data) {
            qDebug() << "Task ID:" << pair.first << ", Duration:" << pair.second;
            arcWidget->duration_Map.insert(pair.first, pair.second);
        }
        QVBoxLayout *layout = new QVBoxLayout(ui->frame);
        layout->addWidget(arcWidget);
        ui->frame->setLayout(layout);
    }
    this->close();
}

void Statistics::on_start_clicked()
{
    tomatoclock *w=new tomatoclock;
    this->close();
    w->show();
}//切换首页


void Statistics::on_task_clicked()
{
    TaskPage *t1=new TaskPage;
    this->close();
    t1->show();
}//切换到任务集


void Statistics::on_wode_clicked()
{
    Wode *w=new Wode;
    this->close();
    w->show();
}//切换到设置

void Statistics::on_pushButton_clicked()
{
    MainWindow *staticsWindow=new MainWindow();
    staticsWindow->show();

}

